//
//  CanTingViewController.m
//  MyProject
//
//  Created by apple on 15/11/9.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "CanTingViewController.h"
#import "AFNetworking.h"
#import "BiChiModel.h"
#import "headModel.h"
#import "JSONModelArray.h"
#import "UIImageView+AFNetworking.h"
#import "LZXHelper.h"
#import "CanTingDetailViewController.h"


#define URL @"http://www.youyuwei.com/api/mustdish/%@"
@interface CanTingViewController ()<UITableViewDataSource,UITableViewDelegate>

{
    NSMutableArray *_dataArr;
    NSMutableArray *headArr;
    JSONModelArray *listArr;
}
@property (nonatomic,strong)UITableView *tableView;
@end

@implementation CanTingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _dataArr = [[NSMutableArray alloc]initWithCapacity:0];
    headArr = [[NSMutableArray alloc]init];
    [self getData];
    
    [self createView];
}

-(void)getData
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:[NSString stringWithFormat:URL,self.ID] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil][@"data"];
        
        NSArray *arr = dic[@"list"];
        listArr = [[JSONModelArray alloc]initWithArray:arr modelClass:[BiChiModel class]];
        headModel *model = [[headModel alloc]initWithDictionary:dic[@"dish"] error:nil];
        [headArr addObject:model];
        [_dataArr addObject:headArr];
        [_dataArr addObject:listArr];
        [self.tableView reloadData];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}

-(void)createView
{
    self.tableView  =[[UITableView alloc]initWithFrame:self.view.bounds style:0];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [_tableView registerNib:[UINib nibWithNibName:@"CanTingHeadCell" bundle:nil] forCellReuseIdentifier:@"111"];
    
    [_tableView registerNib:[UINib nibWithNibName:@"BiChiCell" bundle:nil] forCellReuseIdentifier:@"345"];
    
}

#pragma mark－代理方法- 
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataArr.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[_dataArr objectAtIndex:section] count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *arr = [_dataArr objectAtIndex:indexPath.section];
    if (indexPath.section == 0&& indexPath.row == 0) {
        headModel *model = [arr objectAtIndex:indexPath.row];
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"111"];
        UIImageView *imgV = (UIImageView *)[cell.contentView viewWithTag:2000];
        [imgV setImageWithURL:[NSURL URLWithString:model.imgs] placeholderImage:nil];
        //设置圆角
        imgV.layer.cornerRadius = 60;
        imgV.layer.masksToBounds = YES;
        
        UILabel *lable1 = (UILabel *)[cell.contentView viewWithTag:2001];
        lable1.text = model.title;
        UILabel *lable2 = (UILabel *)[cell.contentView viewWithTag:2003];
        lable2.text = model.sum;
        return cell;
        
    }else{
       BiChiModel *model = [arr objectAtIndex:indexPath.row];
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"345"];
        UIImageView *imgV = (UIImageView *)[cell.contentView viewWithTag:1000];
        [imgV setImageWithURL:[NSURL URLWithString:model.imgs[0]] placeholderImage:nil];
        UILabel *lable1 = (UILabel *)[cell.contentView viewWithTag:1001];
        lable1.text = model.name;
        UILabel *lable2 = (UILabel *)[cell.contentView viewWithTag:1002];
        lable2.text = model.sum;
        UILabel *lable3 = (UILabel *)[cell.contentView viewWithTag:1003];
        lable3.text = model.type;
        
        return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *arr = [_dataArr objectAtIndex:indexPath.section];
    float height = 0;
    if (indexPath.section == 0) {
        headModel *model = [arr objectAtIndex:indexPath.row];
       height = [LZXHelper textHeightFromTextString:model.sum width:[UIScreen mainScreen].bounds.size.width - 60 fontSize:15];
        return 190 + height;
        
    }else{
        BiChiModel *model = [arr objectAtIndex:indexPath.row];
        height = [LZXHelper textHeightFromTextString:model.sum width:[UIScreen mainScreen].bounds.size.width - 20 fontSize:15];
        return 200 + height;
    }
   
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        NSArray *arr = [_dataArr objectAtIndex:1];
        CanTingDetailViewController *canting = [[CanTingDetailViewController alloc]init];
        
        BiChiModel *model = [arr objectAtIndex:indexPath.row];
        canting.ID = model.id;
        
        [self.navigationController pushViewController:canting animated:YES];
    }
   
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
